/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.common.query;

import vip.dexian.common.utils.LocalDateTimeUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import vip.dexian.common.utils.LocalDateTimeUtils;

import java.time.LocalDateTime;

/**
 * 时间类型的范围查询条件
 *
 * @author 挺好的 2023年05月30日 18:44
 */
@Data
@EqualsAndHashCode (callSuper = false)
@SuperBuilder
@NoArgsConstructor
public class LocalDateTimeBetween extends BaseBetween <LocalDateTime, LocalDateTime> {

    /**
     * 创建时间范围的查询条件
     *
     * @param lowerBound
     *         左边的值
     * @param upperBound
     *         右边的值
     *
     * @return {@link LocalDateTimeBetween}
     */
    public static LocalDateTimeBetween of (LocalDateTime lowerBound, LocalDateTime upperBound) {
        return LocalDateTimeBetween.builder().lowerBound(lowerBound).upperBound(upperBound).build();
    }

    /**
     * 创建时间范围的查询条件
     *
     * @param lowerBound
     *         左边的值
     * @param upperBound
     *         右边的值
     * @param isMinMax
     *         是否自动生成min 和 max时间
     *
     * @return {@link LocalDateTimeBetween}
     */
    public static LocalDateTimeBetween of (LocalDateTime lowerBound, LocalDateTime upperBound, boolean isMinMax) {

        if (isMinMax && lowerBound != null && upperBound != null) {

            lowerBound = LocalDateTimeUtils.getMinTime(lowerBound);
            upperBound = LocalDateTimeUtils.getMaxTime(upperBound);

            return LocalDateTimeBetween.builder().lowerBound(lowerBound).upperBound(upperBound).build();
        }

        return of(lowerBound, upperBound);
    }


    /**
     * 是否有效，如果无效，将不添加到搜索条件中。
     * 当 lowerBound == null || upperBound == null ，返回false， 当 lowerBound 在 upperBound之前，返回true，否则返回false。
     * {@link vip.dexian.common.utils.LocalDateTimeUtils#isBefore(java.time.LocalDateTime, java.time.LocalDateTime)} }
     *
     * @return 是否有效
     */
    @Override
    public boolean isValid () {
        LocalDateTime lowerBound = super.getLowerBound();
        LocalDateTime upperBound = super.getUpperBound();

        if (lowerBound == null || upperBound == null) {
            return false;
        }

        return LocalDateTimeUtils.isBefore(lowerBound, upperBound);
    }

    @Override
    public String toString () {
        return super.toString();
    }
}
